package com.ccb.lms.util;

/**
 * @author 胡光伟 分页辅助类，不是基类的，自己可以加泛型
 * 
 */
public class BasePage {
	public static final Integer DEFAULT_CURRENT = 1; // 默认显示第一页
	public static final Integer DEFAULT_PAGE_NUM = 10;// 默认显示10条记录

	protected Integer pageFirRecord = 0; // 当前页第一条记录
	protected Integer currentPage = 1; // 当前页数
	protected Integer totalPages; // 总页数
	protected Integer totalRecord; // 总记录数
	protected Integer showRecordNum = DEFAULT_PAGE_NUM; // 每页显示记录数
	protected Integer showPageNum; // 当前页显示的记录数量
	protected Integer prePage = 1;
	protected Integer nextPage = 1;

	public BasePage() {
		// 不需要做事情，无参数构造方法
	}

	public BasePage(int currentPage, int totalRecord) {
		// 重载1
		this.setTotalRecord(totalRecord);
		this.setTotalPages();
		this.setCurrentPage(currentPage);

		this.setShowPageNum();
		this.setPageFirRecord();
		this.setPrePage();
		this.setNextPage();
	}

	/**
	 * 重载2
	 * 
	 * @param currentPage
	 * @param totalRecord
	 * @param showRecordNum
	 */
	public BasePage(int currentPage, int totalRecord, int showRecordNum) {

		this.setTotalRecord(totalRecord);
		this.setShowRecordNum(showRecordNum);
		this.setTotalPages();
		this.setCurrentPage(currentPage);

		this.setShowPageNum();
		this.setPageFirRecord();

		this.setPrePage(); // 计算前一页页码
		this.setNextPage(); // 计算下一页页码
	}

	public Integer getPrePage() {
		return prePage;
	}

	public void setPrePage() {
		this.prePage = currentPage - 1;
	}

	public Integer getNextPage() {
		return nextPage;
	}

	public void setNextPage() {

		if (currentPage == totalPages) { // 如果当前页码为总页码，即最后一页
			this.nextPage = 0; // 返回0
		} else {
			this.nextPage = currentPage + 1;
		}
		if (totalPages == 0) { // 如果总页数为0，怎么返回0；
			this.nextPage = 0;
		}
	}

	public Integer getShowPageNum() {
		return showPageNum;
	}

	public void setShowPageNum() { // 当前页显示的记录数量
		if (currentPage * showRecordNum - totalRecord > 0) {
			this.showPageNum = totalRecord - (currentPage - 1) * showRecordNum;
		} else {
			this.showPageNum = showRecordNum;
		}

	}

	public Integer getShowRecordNum() {
		return showRecordNum;
	}

	public void setShowRecordNum(Integer showRecordNum) {
		if (showRecordNum == 0) { // 如果记录数为0，则默认为5
			this.showRecordNum = 10;
		} else {
			this.showRecordNum = showRecordNum;
		}

	}

	public Integer getTotalPages() {

		return totalPages;
	}

	public void setTotalPages() { // 计算总页数
		if (totalRecord % showRecordNum == 0) {
			this.totalPages = totalRecord / showRecordNum;
		} else {
			this.totalPages = totalRecord / showRecordNum + 1;
		}

	}

	public Integer getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(Integer totalRecord) {
		this.totalRecord = totalRecord;
	}

	public Integer getCurrentPage() {

		return currentPage;
	}

	public void setCurrentPage(Integer currentPage) {

		if (currentPage == 0 || currentPage < 0) {
			currentPage = 1;
		}
		if (currentPage > totalPages && totalPages != 0) {

			this.currentPage = totalPages; // 当前页大于总页数时为总页数,并且保证不存在记录时不出错，即totalPages!=0
		} else if (totalPages == 0) {
			this.currentPage = 1;

		} else {
			this.currentPage = currentPage;
		}

	}

	public void setPageFirRecord() { // 第一条记录所在集合的标号，比实际排数少一

		this.pageFirRecord = (getCurrentPage() - 1) * showRecordNum;

	}

	public Integer getPageFirRecord() {
		return pageFirRecord;
	}
}